<?php
include("functions.php");
include("libs/smarty.class.php");
$smarty = new Smarty();

$title = "Panel użytkownika.";
$smarty->assign("title",$title);

if(isset($_SESSION['user_id'])){
    
    /*
    $facultyId =  $_SESSION['student_info']['0']['faculty_id'];
    $specializationId = $_SESSION['student_info']['0']['specialization_id'];

    $faculty = getRow("name", $facultyId, "faculty");
    $specialization = getRow("name", $specializationId, "specializations");
    
    $smarty -> assign("specialization",$specialization['0']['name']);
    $smarty -> assign("faculty",$faculty['0']['name']);
    */
    if($_SESSION['user_type'] == 'user_student'){
        $student_id = $_SESSION['user_id'];
        $check = mysql_query("SELECT sp.id FROM student_practices sp WHERE sp.student_id ='$student_id'");
        if(mysql_num_rows($check) > 0 ){
            //CHECK STATUS
            $query = mysql_query("SELECT sp.* FROM student_practices sp WHERE sp.student_id ='$student_id' AND sp.status = '1'");
            while($row = mysql_fetch_assoc($query)){
                $sp[] = $row;
            }
            $status = $sp['0']['status'];
            $practice_id = $sp['0']['practice_id'];
            switch($status){
                case 0 :
                    $info_action = displayInfo(4,'green');
                    break;
                case 1 :
                    $practiceDetails = getPracticeDetails($practice_id);
                    $smarty->assign("p",$practiceDetails['practice']);
                    $smarty->assign("c",$practiceDetails['company']);
                    $smarty->assign("pe",$practiceDetails['person']);
                    $smarty->assign("practiceDetails",$practiceDetails);
                    break;
                case 2 :
                    //PRACTICE FINISHED
                    //$info_action = displayInfo(4,'green');
                    break;
                case -1: break;
            }
        }else{
            $info_action = displayInfo(8,'green');
        }
        //CHECK QUEUE
        /*
        $check = mysql_query("SELECT sp.id FROM student_practices sp WHERE sp.student_id ='$student_id'");
        if(mysql_num_rows($check) == 0){
            //CHECK PRACTICE
            $get_practice_id = getRow("practice_id", $student_id,"student_practice");
            $practice_id = $get_practice_id['0']['practice_id'];

            if($practice_id != 1){
                    $practiceDetails = getPracticeDetails($practice_id);
                    $smarty->assign("p",$practiceDetails['practice']);
                    $smarty->assign("c",$practiceDetails['company']);
                    $smarty->assign("pe",$practiceDetails['person']);
                    $smarty->assign("practiceDetails",$practiceDetails);
                }else{
                    $info_practice = displayInfo(8,'red');
                }
        }else{
            $info_practice = displayInfo(4,'green');
        }
        */

        $smarty->assign("info_action",$info_action);
        $smarty->assign("info_practice",$info_practice);
        $smarty->assign("specializations",$specializations);
        $smarty->assign("faculties",$faculties);
        $smarty->display('user_panel_student.tpl');
    }


    if($_SESSION['user_type'] == 'user_company'){

        if(isset($_POST['delete'])){
            if($_POST['cb']!=''){
                foreach($_POST['cb'] as $key => $value){
                 $sql = mysql_query("DELETE FROM practices WHERE id='$value'");
                }
                $info_action = displayInfo(14, 'red');
            }
        }
        
        $remove_id = $_GET['remove_id'];
        if($remove_id !=''){
            echo deletePractice2($remove_id);
            $info_action = displayInfo(16,'red');
        }

        $finish_id = $_GET['finish_id'];
        if($finish_id !=''){
            echo finishPractice($finish_id);
            $info_action = displayInfo(24,'green');
        }
        
        $company_id = $_SESSION['user_id'];
        $practices_query = mysql_query("SELECT p.id, p.add_date,p.date_start, p.date_end,p.amount,p.paid,p.duties,p.requirements,
                                    p.technology, p.company_id, p.company_person,p.`status` FROM practices p WHERE p.company_id = '$company_id'
                                    ORDER BY p.status");
        while($row = mysql_fetch_assoc($practices_query)){ $practices[] = $row; }

        $students_query = mysql_query("SELECT
              user_student.id,
              user_student.email,
              user_student.name AS student_name,
              user_student.surname AS student_surname,
              user_student.album_nr,
              faculty.name AS faculty,
              specializations.name AS specialization,
              practices.date_start,
              practices.date_end,
              practices.id AS practice_id,
              practices.`status`,
              practices.company_id AS company_id
            FROM
              practices
              INNER JOIN user_student ON (practices.id = user_student.practice_id)
              INNER JOIN specializations ON (user_student.specialization_id = specializations.id)
              INNER JOIN faculty ON (user_student.faculty_id = faculty.id) WHERE practices.company_id = '$company_id'");
        while($row = mysql_fetch_assoc($students_query)){ $students[] = $row; }

        print_r($students);
        $smarty->assign("info_action",$info_action);
        $smarty->assign('practices',$practices);
        $smarty->assign('students',$students);
        $smarty->display('user_panel_company.tpl'); 
    }
}else{
    echo "Musisz być zalogowany!";
}

?>